import * as vscode from 'vscode';
import { SnippetProvider } from './providers/snippetProvider';
import { SnippetManager } from './utils/snippetManager';
import { InsertSnippetCommand } from './commands/insertSnippet';
import { CreateSnippetCommand } from './commands/createSnippet';
import { EditSnippetCommand } from './commands/editSnippet';
import { DeleteSnippetCommand } from './commands/deleteSnippet';
import { ExportSnippetsCommand } from './commands/exportSnippets';
import { ImportSnippetsCommand } from './commands/importSnippets';

export function activate(context: vscode.ExtensionContext) {
    const snippetManager = new SnippetManager(context);
    const snippetProvider = new SnippetProvider(snippetManager);

    vscode.window.registerTreeDataProvider('snippetExplorer', snippetProvider);

    // 注册命令
    const commands = [
        new InsertSnippetCommand(snippetManager),
        new CreateSnippetCommand(snippetManager, snippetProvider),
        new EditSnippetCommand(snippetManager, snippetProvider),
        new DeleteSnippetCommand(snippetManager, snippetProvider),
        new ExportSnippetsCommand(snippetManager),
        new ImportSnippetsCommand(snippetManager, snippetProvider)
    ];

    commands.forEach(command => {
        const disposable = vscode.commands.registerCommand(
            command.commandId,
            (...args) => command.execute(...args)
        );
        context.subscriptions.push(disposable);
    });

    // 监听配置变化
    const configListener = vscode.workspace.onDidChangeConfiguration(e => {
        if (e.affectsConfiguration('snippetManager')) {
            snippetProvider.refresh();
        }
    });

    context.subscriptions.push(configListener);
}

export function deactivate() {}
